home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / Perl_WWW_Utilities / MHonArc / lib / mhtxtsetext.pl < prev    next >
Encoding:
Perl Script  |  1995-03-03  |  4.8 KB  |  148 lines

  1. ##---------------------------------------------------------------------------##
  2. ## Library to convert text/setext to HTML.  Adapted for use in MHonArc
  3. ## by ehood@convex.com, Sept 1994.
  4. ## Filter routine can be registered with the following:
  5. ##        <MIMEFILTERS>
  6. ##        text/setext:m2h_text_setext'filter:mhtxtsetext.pl
  7. ##        text/x-setext:m2h_text_setext'filter:mhtxtsetext.pl
  8. ##        </MIMEFILTERS>
  9. ##---------------------------------------------------------------------------##
  10. # setext -> HTML converter
  11. #
  12. # $Id: setext.pl,v 2.8 1994/06/23 05:44:15 sanders Exp $
  13. #
  14. # Tony Sanders <sanders@earth.com>, June 1993
  15. #
  16. # Status of typotags:
  17. #     header-tt         passed untouched (XXX: use Subject: in next release)
  18. #     title-tt          <H1>...</H1> (and <TITLE> if needed)
  19. #     subhead-tt        <H2>...</H2> (and <TITLE> if needed)
  20. #     indent-tt         reflows paragraphs
  21. #
  22. #     bold-tt           <B>...</B>
  23. #     italic-tt         <I>...</I>
  24. #     underline-tt      <I>...</I>
  25. #     hot-tt            <A HREF="...">...</A>           (see also href-tt)
  26. #
  27. #     quote-tt          <BLOCKQUOTE>...</BLOCKQUOTE>
  28. # NIY bullet-tt         <UL>...</UL>
  29. #
  30. #     twobuck-tt        ignored
  31. #     suppress-tt       suppressed in output
  32. #     twodot-tt         ignored
  33. #
  34. # Additional typotags supported for HTML:
  35. #     href-tt           .. _text HREF
  36. #     isindex-tt        .. <isindex>
  37. #
  38. # setext'html -- converts setext (.etx files) to HTML
  39. # setext'title -- utility routine to convert setext titles and subheads to HTML
  40. #
  41.  
  42. # TODO:XXX
  43. # I need to figure out how to allow HTML markup in the text while at the
  44. # same time suppresing "unintentional" markup.  For now < & > are HTML'ized.
  45.  
  46. # Define the translations supported
  47. # $trans{'text/setext'}            = "text/html:setext'html";
  48.  
  49. package m2h_text_setext;
  50.  
  51. # parser states
  52. $FMT = 0;       # in free flow text (normal HTML mode)
  53. $PRE = 1;       # in preformated text <PRE>...</PRE>
  54. $QUOTE = 2;     # in blockquote <BLOCKQUOTE>...</BLOCKQUOTE>
  55.  
  56. sub filter {
  57.     local($header, *fields, *body) = @_;
  58.     local(@data) = split(/\n/,$body);
  59.  
  60.     $ret = '';
  61.     # first pass, process <HEAD> items and hypertext link information
  62.     for ($i = 0; $i <= $#data; $i++) {
  63.         $_ = $data[$i];                 # $_ is default for m//
  64.  
  65.         # <ISINDEX> must be inside <HEAD>...</HEAD>
  66.         /^\.\.\s+<isindex>/i &&
  67.             do { $data[$i] = ".."; next; };
  68.  
  69.         # locate HREF's:  .. _href URL
  70.         /^\.\.\s+_([^\s]*)\s+(.*)\s*/ && do { $href{$1} = $2; next; };
  71.  
  72.         # first title-tt or subhead-tt gets <TITLE>...</TITLE>
  73.         # &title also adds the <H#>...</H#> to the appropriate line
  74.         /^===/ && do { &title("H1", $i); next; };
  75.         /^---/ && do { &title("H2", $i); next; };
  76.     }
  77.  
  78.     # second pass, handle remaining typotags
  79.     $curstate = $FMT;
  80.     foreach (@data) {
  81.         # process title information
  82.         /^\.\.\s+(<H.>)(.*)(<\/H.>)/i && do {
  83.             &to_fmt; $ret .= $1. &htmlize($2). $3. "\n"; next; };
  84.         next if /^\.\./;
  85.  
  86.         # handle line breaks
  87.         if ($curstate == $FMT && /^\s*$/) {
  88.             $ret .= "<P>\n" unless $fold++; next; }
  89.         $fold = 0;
  90.  
  91.         # state transitions
  92.         if (/^>\s/) { &to_quote; }
  93.         elsif (/^  [^ ]/) { &to_fmt; }
  94.         else { &to_pre; }
  95.  
  96.         s/^>\s*//;                                              # fix quote-tt
  97.         s/^  ([^ ])/\1/;                                        # fix indent-tt
  98.  
  99.         # bold-tt
  100.             s#\*\*([^\*]*)\*\*#\376B\377$1\376/B\377#;
  101.         # italic-tt
  102.             s#~([^~]*)~#\376I\377$1\376/I\377#;
  103.         # hot-tt
  104.             s#\b([^\s]*)_\b#
  105.                 $h = $href{$1}; ($a = $1) =~ s,_, ,g;
  106.                 $h ? qq'\376A HREF="$h"\377$a\376/A\377' : "\376I\377$a\376/I\377"; #e;
  107.         # underline-tt
  108.             s#_([^\s]*)_#
  109.                 ($a = $1) =~ s,_, ,g; "\376I\377$a\376/I\377"; #e;
  110.         $ret .= &htmlize($_). "\n";
  111.     }
  112.     &to_fmt;
  113.     ($ret);
  114. }
  115.  
  116. sub to_fmt {
  117.     return if $curstate == $FMT;
  118.     $ret .= "</PRE>\n" if $curstate == $PRE;
  119.     $ret .= "</PRE></BLOCKQUOTE>\n" if $curstate == $QUOTE;       #XXX
  120.     $curstate = $FMT;
  121. }
  122. sub to_pre {
  123.     return if $curstate == $PRE;
  124.     $ret .= "<PRE>\n" if $curstate == $FMT;
  125.     $ret .= "</PRE></BLOCKQUOTE><PRE>\n" if $curstate == $QUOTE;  #XXX
  126.     $curstate = $PRE;
  127. }
  128. sub to_quote {
  129.     return if $curstate == $QUOTE;
  130.     $ret .= "<BLOCKQUOTE><PRE>\n" if $curstate == $FMT;           #XXX
  131.     $ret .= "</PRE><BLOCKQUOTE><PRE>\n" if $curstate == $PRE;     #XXX
  132.     $curstate = $QUOTE;
  133. }
  134. sub htmlize {
  135.     local($_) = @_;
  136.     s/\&/\&\#38\;/g; s/\</\&\#60\;/g; s/\>/\&\#62\;/g;
  137.     s/\376/</g; s/\377/>/g;                                     # convert back
  138.     $_;
  139. }
  140. sub title {
  141.     local($head, $i) = @_;
  142.     $data[$i--] = ".."; $data[$i] =~ s/^\s*//;
  143.     # $ret .= "<TITLE>$data[$i]</TITLE>\n" unless $title++;
  144.     $data[$i] = ".. <$head>" . $data[$i] . "</$head>";
  145. }
  146.  
  147. 1;
  148.